package com.zr.influx.scheduler;

import com.zr.influx.model.ForexData;
import com.zr.influx.service.ForexDataGenerator;
import com.zr.influx.service.InfluxDBService;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务调度器，定期生成并写入外汇行情数据
 */
@Component
@RequiredArgsConstructor
public class ForexDataScheduler {

    private static final Logger logger = LoggerFactory.getLogger(ForexDataScheduler.class);

    private final ForexDataGenerator dataGenerator;
    private final InfluxDBService influxDBService;

    /**
     * 定时任务：生成外汇行情数据并写入InfluxDB
     * 执行频率由配置文件中的forex.schedule.fixed-delay指定
     */
    @Scheduled(fixedDelayString = "${forex.schedule.fixed-delay}")
    public void generateAndSaveForexData() {
        logger.info("开始生成外汇行情数据...");

        // 生成所有货币对的行情数据
        List<ForexData> forexDataList = dataGenerator.generateAllCurrencyPairsData();

        // 批量写入InfluxDB
        influxDBService.batchWriteForexData(forexDataList);

        logger.info("外汇行情数据生成和写入完成");
    }
}

